package com.bw.day10.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bw.day10.dao.PaybillDao;
import com.bw.day10.entity.Organization;
import com.bw.day10.entity.Paybill;
import com.bw.day10.entity.User;
import com.bw.day10.service.PaybillService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class PaybillServiceImpl implements PaybillService{
	@Autowired
	private PaybillDao paybillDao;
	@Override
	public PageInfo<Paybill> getlist(Integer pageNum, Integer pageSize, Paybill paybill) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		List<Paybill> paybills = paybillDao.getlist(paybill);
		return new PageInfo<Paybill>(paybills);
	}
	@Override
	public Paybill selectid(Integer id) {
		// TODO Auto-generated method stub
		Paybill paybill = paybillDao.selectid(id);
		return paybill;
	}
	@Override
	public List<Organization> getorganization() {
		// TODO Auto-generated method stub
		List<Organization> organizations = paybillDao.getorganization();
		return organizations;
	}
	@Override
	public void inserpaybill(Paybill paybill) {
		// TODO Auto-generated method stub
		List<User> collect = paybill.getUsers().stream().filter(usern->usern.getName()!=null).collect(Collectors.toList());
		paybill.setUsers(collect);
		if (paybill.getId()!=null && paybill.getId()!=0) {
			System.out.println(paybill);
			paybillDao.deleteuser(paybill.getId());
			paybillDao.deletebilluser(paybill.getId());
			paybillDao.deletepaybill(paybill.getId());
			paybillDao.inserpaybill(paybill);
			System.out.println(paybill);
			for (User user : paybill.getUsers()) {
				paybillDao.inseruser(user);
				paybillDao.inserbilluser(paybill.getId(),user.getId());
				
			}
			
		}else {
			paybillDao.inserpaybill(paybill);
			System.out.println(paybill);
			for (User user : paybill.getUsers()) {
				paybillDao.inseruser(user);
				paybillDao.inserbilluser(paybill.getId(),user.getId());
				
			}
		}
	}
	@Override
	public void deletes(Integer id) {
		// TODO Auto-generated method stub
		paybillDao.deleteuser(id);
		paybillDao.deletebilluser(id);
		paybillDao.deletepaybill(id);
	}
	@Override
	public User login(User user) {
		// TODO Auto-generated method stub
		User user2 = paybillDao.login(user.getName());
		if (user2!=null) {
			if (user2.getName().equals(user.getName())) {
				return user2;
			}
		}
		return null;
	}

}
